package com.ms.p2p.controller.admin;


import com.alibaba.dubbo.config.annotation.Reference;
import com.ms.common.utils.excel.ExcelData;
import com.ms.p2p.bean.PageBean;
import com.ms.p2p.controller.BaseController;
import com.ms.p2p.provider.MemberAccountBankProvider;
import com.ms.p2p.utils.JsonDataGridHelper;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

/**
 * 用户银行卡信息
 */
@Controller("AdminBankController")
@RequestMapping({"/admin/bank"})
public class AdminBankController extends AdminBaseController {

    @Reference
    private MemberAccountBankProvider memberAccountBankProvider;

    @RequestMapping(value = {"/queryData"}, produces = {"application/json;charset=UTF-8"})
    @ResponseBody
    public PageBean queryData(HttpServletRequest request, HttpSession session
            , String userName, String userId,
                              PageBean page
    ) {

        page.setList(memberAccountBankProvider.findPager(userId, userName, page));
        page.setTotalCount(memberAccountBankProvider.findCount(userId, userName));
        return JsonDataGridHelper.createJSONData(page);
    }







    @RequestMapping({"/list"})
    public String list(String userName, String userId) {
        return "admin/account/record/bank/list";
    }


    @RequestMapping({"/exportList"})
    public String exportList(String userName, String userId, Model model) {
        List<Map<String, Object>> bankCards = memberAccountBankProvider.findAll(userId, userName);

        for (Map<String, Object> map : bankCards) {
            String status = String.valueOf(map.get("status"));
            map.put("status", "0".equals(status) ? "绑定中" : "已绑定");
        }

        ExcelData excelData = new ExcelData();
        excelData.setHeads(new String[]{"用户ID", "用户名", "真实姓名/企业名称", "开户银行", "开户支行", "银行卡号", "状态"});
        excelData.setFieldNames(new String[]{"user_id", "userName", "realName", "bank", "branch", "bank_cardno", "status"});
        excelData.setDataList(bankCards);
        excelData.setModelClass(Map.class);
        model.addAttribute("excelData", excelData);
        model.addAttribute("fileName", "用户银行卡信息");
        return "excel";
    }

    @Override
    public boolean getBinderFlag() {
        return true;
    }
}
